﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;
using vncom.Models;

namespace vncom.Controllers
{
    public class AdministratorController : Controller
    {
        //
        // GET: /Administrator/

        public ActionResult Index()
        {
            return View();
        }

        #region "Danh sach template"
        // Danh sach template quan lý
        public ActionResult ThietLapTemplate()
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter DMLinhVucID = new SqlParameter("@DMLinhVucID", SqlDbType.VarChar);
            DMLinhVucID.Value = 0;
            Pram.Add(DMLinhVucID);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.VarChar);
            HoatDong.Value = 1;
            Pram.Add(HoatDong);
            var dt = DataProvider.SelectStoreProcedure("M_TemplateSYS_Lst", Pram);
            ViewData["TemplateSYS_List"] = dt;

            M_Template_SYS_Edit editTemplate = new M_Template_SYS_Edit();
            editTemplate.ListDanhMucLinhVuc = getDanhMucLinhVuc();
            editTemplate.HoatDong = true;
            return View(editTemplate);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThietLapTemplate(M_Template_SYS_Edit SYS_Template, string SYS_DanhMucLinhVuc)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter DMLinhVucID = new SqlParameter("@DMLinhVucID", SqlDbType.VarChar);
            DMLinhVucID.Value = int.Parse(SYS_DanhMucLinhVuc);
            Pram.Add(DMLinhVucID);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.VarChar);
            HoatDong.Value = SYS_Template.HoatDong;
            Pram.Add(HoatDong);
            var dt = DataProvider.SelectStoreProcedure("M_TemplateSYS_Lst", Pram);
            ViewData["TemplateSYS_List"] = dt;

            M_Template_SYS_Edit editTemplate = new M_Template_SYS_Edit();
            editTemplate.ListDanhMucLinhVuc = getDanhMucLinhVuc();
            editTemplate.DMLinhVucID = int.Parse(SYS_DanhMucLinhVuc);
            return View(editTemplate);
        }
        #endregion

        #region "Them moi template"
        //Thêm mới một template
        public ActionResult TemplateCreate()
        {
            M_Template_SYS_Edit editTemplate = new M_Template_SYS_Edit();
            editTemplate.ListDanhMucLinhVuc = getDanhMucLinhVuc();
            return View(editTemplate);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult TemplateCreate(M_Template_SYS_Edit SYS_Template, string SYS_DanhMucLinhVuc, string hdTemplateID)
        {
            SYS_Template.ListDanhMucLinhVuc = getDanhMucLinhVuc();

            if (hdTemplateID == "0" || hdTemplateID == null)
            {
                var Pram = setDuLieu(SYS_Template, SYS_DanhMucLinhVuc);
                SYS_Template.DMLinhVucID = int.Parse(SYS_DanhMucLinhVuc);
                int id = DataProvider.Insert("M_TemplateSYS_Ins", Pram, "@TemplateID");
                if (id != 0)
                {
                    System.Web.HttpContext.Current.Response.Write("<script> alert('Bạn đã thêm thành công !');</script>");
                    SYS_Template.TemplateID = int.Parse(id.ToString());
                }
            }
            else
            {
                var Pram = setDuLieu(SYS_Template, SYS_DanhMucLinhVuc, hdTemplateID);
                DataProvider.NonqueryProc("M_TemplateSYS_Upd", Pram);
                SYS_Template.DMLinhVucID = int.Parse(SYS_DanhMucLinhVuc);
                SYS_Template.TemplateID = int.Parse(hdTemplateID);
                System.Web.HttpContext.Current.Response.Write("<script> alert('Bạn cập nhật thành công !');</script>");
            }
            return View(SYS_Template);
        }
        #endregion

        #region "Thay doi template"
        //Thêm mới một template
        public ActionResult TemplateEdit(string id)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter TemplateID = new SqlParameter("@TemplateID", SqlDbType.Int);
            TemplateID.Value = id;
            Pram.Add(TemplateID);           
            DataTable dt = new DataTable();
            dt = DataProvider.SelectStoreProcedure("M_TemplateSYS_Get", Pram);
            M_Template_SYS_Edit TED = new M_Template_SYS_Edit();
            if (dt.Rows.Count > 0)
            {
                TED.TemplateID = int.Parse(dt.Rows[0]["TemplateID"].ToString());
                TED.DMLinhVucID = int.Parse(dt.Rows[0]["DMLinhVucID"].ToString());
                TED.Layout = dt.Rows[0]["Layout"].ToString();
                TED.HoatDong = (Boolean)dt.Rows[0]["HoatDong"];
            }
            TED.ListDanhMucLinhVuc = getDanhMucLinhVuc();
            return View(TED);
        }
        #endregion

        public List<SqlParameter> setDuLieu(M_Template_SYS_Edit SYS_Template, string SYS_DanhMucLinhVuc)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter DMLinhVucID = new SqlParameter("@DMLinhVucID", SqlDbType.Int);
            DMLinhVucID.Value = SYS_DanhMucLinhVuc;
            Pram.Add(DMLinhVucID);
            SqlParameter Layout = new SqlParameter("@Layout", SqlDbType.NVarChar);
            Layout.Value = SYS_Template.Layout;
            Pram.Add(Layout);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.Bit);
            HoatDong.Value = SYS_Template.HoatDong;
            Pram.Add(HoatDong);
            return Pram;
        }

        public List<SqlParameter> setDuLieu(M_Template_SYS_Edit SYS_Template, string SYS_DanhMucLinhVuc, string hdTemplateID)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter TemplateID = new SqlParameter("@TemplateID", SqlDbType.Int);
            TemplateID.Value = hdTemplateID;
            Pram.Add(TemplateID);
            SqlParameter DMLinhVucID = new SqlParameter("@DMLinhVucID", SqlDbType.Int);
            DMLinhVucID.Value = SYS_DanhMucLinhVuc;
            Pram.Add(DMLinhVucID);
            SqlParameter Layout = new SqlParameter("@Layout", SqlDbType.NVarChar);
            Layout.Value = SYS_Template.Layout;
            Pram.Add(Layout);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.Bit);
            HoatDong.Value = SYS_Template.HoatDong;
            Pram.Add(HoatDong);
            return Pram;
        }

        public List<SYS_DanhMucLinhVuc> getDanhMucLinhVuc()
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter DanhMucLinhVucID = new SqlParameter("@DanhMucLinhVucID", SqlDbType.Int);
            DanhMucLinhVucID.Value = 0;
            Pram.Add(DanhMucLinhVucID);
            SqlParameter ParentID = new SqlParameter("@ParentID", SqlDbType.Int);
            ParentID.Value = 0;
            Pram.Add(ParentID);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.Bit);
            HoatDong.Value = true;
            Pram.Add(HoatDong);
            DataTable dt = new DataTable();
            dt = DataProvider.SelectStoreProcedure("SYS_DanhMucLinhVuc_Lst", Pram);
            List<SYS_DanhMucLinhVuc> listDM = new List<SYS_DanhMucLinhVuc>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SYS_DanhMucLinhVuc DMLV = new SYS_DanhMucLinhVuc();
                DMLV.DanhMucLinhVucID = int.Parse(dt.Rows[i]["DanhMucLinhVucID"].ToString());
                DMLV.TenLinhVuc = dt.Rows[i]["TenLinhVuc"].ToString();
                listDM.Add(DMLV);
            }
            return listDM;
        }
    }
}
